<template>
  <el-container>
    <el-dialog
      title="登录"
      visible
      center
      width="30%"
      :append-to-body="true"
      :before-close="handleClose"
    >
      <el-form
        ref="loginformref"
        :model="loginform"
        :rules="loginformrules"
        :hide-required-asterisk="true"
      >
        <el-form-item
          prop="username"
          label="用户名"
          label-width="100px"
          class="loginlist"
        >
          <el-input v-model="loginform.username"></el-input>
        </el-form-item>
        <el-form-item
          prop="password"
          label="密码"
          label-width="100px"
          class="passwordlist"
        >
          <el-input
            v-model="loginform.password"
            type="password"
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button
          type="primary"
          @click="login"
        >登录</el-button>
        <el-button
          type="info"
          @click="resetlogin"
        >重置</el-button>
      </div>
    </el-dialog>
  </el-container>
</template>

<script>
import { request } from 'network/request'
export default {
  name: 'Login',
  data () {
    return {
      input: '',
      loginform: {
        username: '',
        password: ''
      },
      loginformrules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleClose () {
      this.$router.push('/home')
    },
    resetlogin () {
      this.$refs.loginformref.resetFields()
      this.username = ''
      this.password = ''
    },
    login () {
      this.$refs.loginformref.validate(async valid => {
        if (!valid) {
          this.$message({
            message: '请输入正确的登录格式',
            type: 'error',
            duration: 3000
          })
        } else {
          // 发起登录请求
          request({
            url: '/user/login',
            method: 'post',
            data: this.loginform
          }).then(res => {
            this.$message({
              message: '登录成功',
              type: 'success',
              duration: 3000
            })
            // console.log(res.data.token)
            sessionStorage.setItem('token', res.data.token)
            sessionStorage.setItem('username', res.data.token.split(':')[0])
            setTimeout(() => {
              console.log(this.$route.query.redirect)
              if (this.$route.query.redirect) {
                this.$router.push(this.$route.query.redirect)
              } else {
                this.$router.push('/home')
              }
            }, 2000)
          }).catch(err => {
            if (err) {
              this.$message({
                message: '登录失败',
                type: 'error',
                duration: 3000
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_box {
  background-color: #fff;
  position: absolute;
  left: 50%;
  transform: translate(-50%, 30%);
}

.loginlist {
  margin-top: 30px;
  padding-right: 70px;
}
.passwordlist {
  padding-right: 70px;
}
</style>
